/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.item.etcitem.action;

import com.lineage.server.model.Getback;
import com.lineage.server.model.L1CastleLocation;
import com.lineage.server.model.L1Clan;
import com.lineage.server.model.L1HouseLocation;
import com.lineage.server.model.L1ItemDelay;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.model.L1TownLocation;
import com.lineage.server.model.L1World;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.item.ItemExecutor;
import com.lineage.server.packetserver.S_ServerMessage;

/**
 * Class <code>TeleportsScrollHomeClan</code> 血盟传送卷轴 - 40124.
 * 
 * @author jrwz
 * @version 2012-6-22上午09:16:00
 * @see com.lineage.server.model.item.etcitem.action
 * @since JDK1.7
 */
public final class TeleportsScrollHomeClan extends ItemExecutor {

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static ItemExecutor get() {
        return new TeleportsScrollHomeClan();
    }

    /** 血盟传送卷轴 - 40124. */
    private TeleportsScrollHomeClan() {
    }

    @Override
    public void execute(final L1PcInstance pc, final L1ItemInstance useItem,
            final int readC1, final int readC2, final int targetId,
            final int targetX, final int targetY) {
        if (pc.getMap().isEscapable() || pc.isGm()) {
            int castle_id = 0;
            int house_id = 0;

            // 有血盟
            if (pc.getClanid() != 0) {
                final L1Clan clan = L1World.getInstance().getClan(
                        pc.getClanname());
                if (clan != null) {
                    castle_id = clan.getCastleId(); // 城堡ID
                    house_id = clan.getHouseId(); // 盟屋ID
                }
            }

            // 有城堡回城堡
            if (castle_id != 0) {
                if (pc.getMap().isEscapable() || pc.isGm()) {
                    int[] loc = new int[3];
                    loc = L1CastleLocation.getCastleLoc(castle_id);
                    final int locx = loc[0];
                    final int locy = loc[1];
                    final int mapid = (loc[2]);
                    L1Teleport.teleport(pc, locx, locy, mapid, 5, true);
                    L1ItemDelay.teleportUnlock(pc, useItem); // 卷轴传送后 使用物品延迟完才解开停止状态
                    pc.getInventory().removeItem(useItem, 1);
                } else {
                    pc.sendPackets(new S_ServerMessage(647));
                }
            }

            // 有盟屋回盟屋
            else if (house_id != 0) {
                if (pc.getMap().isEscapable() || pc.isGm()) {
                    int[] loc = new int[3];
                    loc = L1HouseLocation.getHouseLoc(house_id);
                    final int locx = loc[0];
                    final int locy = loc[1];
                    final int mapid = (loc[2]);
                    L1Teleport.teleport(pc, locx, locy, mapid, 5, true);
                    L1ItemDelay.teleportUnlock(pc, useItem); // 卷轴传送后 使用物品延迟完才解开停止状态
                    pc.getInventory().removeItem(useItem, 1);
                } else {
                    pc.sendPackets(new S_ServerMessage(647));
                }
            }

            // 都没有就回村庄
            else {

                // 有登记村庄
                if (pc.getHomeTownId() > 0) {
                    final int[] loc = L1TownLocation.getGetBackLoc(pc
                            .getHomeTownId());
                    final int locx = loc[0];
                    final int locy = loc[1];
                    final int mapid = (loc[2]);
                    L1Teleport.teleport(pc, locx, locy, mapid, 5, true);
                    L1ItemDelay.teleportUnlock(pc, useItem); // 卷轴传送后 使用物品延迟完才解开停止状态
                    pc.getInventory().removeItem(useItem, 1);
                } // 无登记村庄
                else {
                    final int[] loc = Getback.GetBack_Location(pc, true);
                    L1Teleport.teleport(pc, loc[0], loc[1], loc[2], 5, true);
                    L1ItemDelay.teleportUnlock(pc, useItem); // 卷轴传送后 使用物品延迟完才解开停止状态
                    pc.getInventory().removeItem(useItem, 1);
                }
            }
        } else {
            pc.sendPackets(new S_ServerMessage(647)); // 这附近的能量影响到瞬间移动。在此地无法使用瞬间移动。
        }
    }
}
